/**
 * 
 */
package com.geekteam.masterenglish.ui.fragment;

import java.util.ArrayList;
import java.util.List;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.AdapterView;

import com.geekteam.masterenglish.Adapter.AlbumAdapter;
import com.geekteam.masterenglish.provider.EEContract.Lession;

/**
 * @author MTROL
 *
 */
public class ListLessionFragment extends BaseListFragment{
	private List<String> mListIdLession;
	
	public ListLessionFragment() {
		super(AlbumAdapter.TYPE_LESSION);
	}

	@Override
	public Loader<Cursor> onCreateLoader(int type, Bundle bundle) {
		CursorLoader loader = null;
		loader = new CursorLoader(getActivity(), Lession.buildLesstionAlbum("IWED0CBF"), AlbumQuerry.PROJECT, null, null, null);
		
		return loader;
	}

	@Override
	public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
		if (getActivity() == null) {
			cursor.close();
			return;
		}
		mAdapter.changeCursor(cursor);
		mListIdLession = new ArrayList<String>();
		cursor.moveToFirst();
		do{
			mListIdLession.add(cursor.getString(cursor.getColumnIndex(Lession.ID)));
		}while(cursor.moveToNext());
	}

	@Override
	public void onLoaderReset(Loader<Cursor> loader) {}
	
	private interface AlbumQuerry{
		String[] PROJECT = {
				Lession._ID,
				Lession.ID,
				Lession.TITLE,
				Lession.COVER,
				Lession.TOTAL_LISTEN,
				Lession.ALBUM_ID
		};
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		PagerLessionFragment fragment = new PagerLessionFragment(position, mListIdLession);
		mActivity.switchFragment(fragment);
	}
}
